package com.gzw.dms.KJ;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.gzw.dms.R;
import com.gzw.dms.adapter.FPOrderAdapter;
import com.gzw.dms.adapter.SelectCustomerAdapter;
import com.gzw.dms.adapter.SelectDepartAdapter;
import com.gzw.dms.base.BaseActivity;
import com.gzw.dms.bean.CuotomerInfo;
import com.gzw.dms.utils.HttpUtils;
import com.gzw.dms.utils.JsonUtil;
import com.gzw.dms.utils.T;
import com.gzw.dms.utils.UrlUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SelectCustomerActivity extends BaseActivity {
    private Context mContext;
    private ListView lv_depart;
    private Button btn_query,btn_sure;
    private EditText et_depart;
    private List<CuotomerInfo> ls;
    private SelectCustomerAdapter arr_adapter;
    private String tag = "SelectDepartActivity---->";
    private List<String> ls_map;
    private List<String> ls_name;
    private String type = "0";
    private Intent mIntent;
    private LinearLayout layout_sure;
    private LinearLayout layout_seect;
    private TextView tv_seleteAll;
    private boolean isCheck = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_select_customer);

    }

    @Override
    protected void initData() {
        mContext = this;
        mIntent = getIntent();
        ls_map = new ArrayList<>();
        ls_name = new ArrayList<>();
        type=(String)mIntent.getExtras().get("type");
        if (type.equals("1")){
            layout_sure.setVisibility(View.GONE);
            layout_seect.setVisibility(View.GONE);
        }
    }

    public void initViews() {
        lv_depart = (ListView) findViewById(R.id.lv_depart);
        btn_query = (Button) findViewById(R.id.btn_query);
        et_depart = (EditText) findViewById(R.id.et_depart);
        btn_sure = (Button) findViewById(R.id.btn_sure);
        layout_sure = (LinearLayout) findViewById(R.id.layout_sure);
        layout_seect = (LinearLayout) findViewById(R.id.layout_selectall);
        tv_seleteAll = (TextView) findViewById(R.id.ic_head1).findViewById(R.id.tv_selectAll);

    }

    @Override
    protected void initEvents() {
        // TODO Auto-generated method stub
        btn_query.setOnClickListener(new MyOnClick());
        lv_depart.setOnItemClickListener(new myOnItemClickListener());
        btn_sure.setOnClickListener(new MyOnClick());
        tv_seleteAll.setOnClickListener(new MyOnClick());

    }

    class MyOnClick implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_sure:
                    ls_map.clear();
                    if (ls != null) {
                        for (int i = 0; i < ls.size(); i++) {
                            if (arr_adapter.getIsSelected().get(i)) {
                                ls_map.add(ls.get(i).getCustomer_id());
                                ls_name.add(ls.get(i).getCustomer_name());
                            }
                        }

                        if (ls_map.size() == 0) {
                            T.showShort(mContext, "请先选择部门!");
                        } else {
                            Intent intent = new Intent();
                            String id = ls_map.toString();
                            String name = ls_name.toString();
                            intent.putExtra("id", id.substring(1, name.length() - 1));
                            intent.putExtra("name", name.substring(1, name.length() - 1));
                            setResult(3, intent);
                            finish();
                        }
                    }
                    break;
                case R.id.tv_selectAll:
                    if (ls != null) {
                        if (isCheck == false) {
                            // 遍历list的长度，将MyAdapter中的map值全部设为true
                            for (int i = 0; i < ls.size(); i++) {
                                SelectCustomerAdapter.getIsSelected().put(i, true);
                            }
                            dataChanged();
                            //改变状态
                            isCheck = true;
                        } else {
                            // 遍历list的长度，将已选的按钮设为未选
                            for (int i = 0; i < ls.size(); i++) {
                                if (SelectCustomerAdapter.getIsSelected().get(i)) {
                                    SelectCustomerAdapter.getIsSelected().put(i, false);
                                }
                            }
                            dataChanged();
                            isCheck = false;
                        }
                    }
                    break;
                default:
                    getDepartment();
                    break;
            }

        }
    }
    // 刷新listview和TextView的显示
    private void dataChanged() {
        // 通知listView刷新
        arr_adapter.notifyDataSetChanged();
    }
    class myOnItemClickListener implements AdapterView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {
            Intent intent = new Intent();
            intent.putExtra("id", ls.get(position).getCustomer_id()
                    .toString());
            intent.putExtra("name", ls.get(position).getCustomer_name()
                    .toString());
            setResult(3, intent);
            finish();
        }
    }

    public void getDepartment() {
        if (isNull(et_depart)) {
            ShowOrderTask showOrderTask = new ShowOrderTask();
            showOrderTask.execute("");
        }

        ShowOrderTask showOrderTask = new ShowOrderTask();
        showOrderTask.execute(et_depart.getText().toString().trim());
    }



    public class ShowOrderTask extends AsyncTask<String, Void, String> {
        //onPreExecute方法用于在执行后台任务前做一些UI操作
        @Override
        protected void onPreExecute() {

        }

        protected String doInBackground(String... params) {

            Map<String, String> map = new HashMap<>();
            map.put("customersName", params[0]);
            String json = JsonUtil.toJson(map);
            return HttpUtils.doPost(UrlUtils.getInstance().getJavaUrl("appReverseGetCustomersInfo"), "json=" + json);
        }

        @Override
        protected void onPostExecute(final String result) {

            if (isNull(result)) {
                T.showShort(mContext, "连接服务器异常");
            } else {
                if (result.indexOf("errorMsg") > 0) {
                    String errorMsg = JsonUtil.getMsg(result, "errorMsg");
                    T.showShort(mContext, errorMsg);
                } else {
                    String successMsg = JsonUtil.getMsg(result, "successMsg");
                    ls = JsonUtil.getPersons(successMsg, new TypeToken<List<CuotomerInfo>>() {
                    }.getType());
                    if (ls.size()==0){
                        T.showShort(mContext, "没有查到相关信息！");
                        arr_adapter = new SelectCustomerAdapter(mContext, ls,type);
                        lv_depart.setAdapter(arr_adapter);
                    }else {
                    arr_adapter = new SelectCustomerAdapter(mContext, ls,type);
                    lv_depart.setAdapter(arr_adapter);
                }
                }
            }
        }
    }

}
